// pages/helpsubmit/helpsubmit.js
Page({

  data: {
    //文本介绍
    intro:'',
    //图片
    picurls:[],
  },
  intro(e){
    this.setData({
      intro:e.detail.value
    })
  },
  //添加照片
  selectpic(e){
    let that = this;
    wx.chooseImage({
      count: 9,
      success(res){
        var picurls = that.data.picurls.concat(res.tempFilePaths)
        that.setData({
          picurls:picurls
        })
      }
    })
  },
  //删除照片
  delpic(e){
    console.log(e)
    let that = this;
    var index = e.currentTarget.dataset.index;
    var del = this.data.picurls
    wx.showModal({
      content: '是否删除该照片',
      title: '提示',
      success(res){
        if(res.confirm){
          del.splice(index,1)
          that.setData({
            picurls:del
          })
      console.log(that.data.picurls)
        }
      },
    })  
  },
  // 查看图片
  showpic(e){
    var index  =e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.picurls[index],     //当前图片地址
      urls: this.data.picurls,                 //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  helpsubmit(e){
    var that = this;
    wx.showModal({
      title: '确定提交求租信息吗？',
      complete: (res) => {
        if (res.confirm) {
          var arr = [];
      var  h = this.data.picurls.length;
      var i =0;
      for (i = 0; i < h; i++) {
      //上传文件
      // console.log(i,h)
      wx.uploadFile({
        url: 'http://127.0.0.1:8080/image/upload',
        filePath: this.data.picurls[i],
          name: "file",
          header: {
            "content-type": "multipart/form-data"
          },
          success: function ({ data: res }) {
            console.log(res)
            res = JSON.parse(res);
            const pic_url = 'http://127.0.0.1:8080/image/download?name=' + res.data
            // console.log(pic_url)
            // 图片路径集合
            arr.unshift(pic_url);
          },
          fail: function (err) {
            wx.showToast({
              title: "上传失败",
              icon: "error",
              duration: 2000
            })
          },
          complete: function (result) {
            // console.log(result.errMsg)
          }
        })
      }
      
      setTimeout(function(){
        var data = that.data;
        var nowtime = new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0,8);
        var newDateTime=new Date().getTime();
        var data1={
          helpinfoid:'h'+newDateTime,
          helptext:data.intro,
          helppic:JSON.stringify(arr),
          helptime:nowtime,
          userid:wx.getStorageSync('openid')
        }
        console.log(data1)
          wx.request({
            url: 'http://127.0.0.1:8080/helpInfo/addHelpInfo',
            method:"POST",
            data:data1,
            success(res){
              // console.log(res)
              if(res.data){
                wx.navigateBack({
                  delta: 1,
                  success: (res) => {
                    wx.showToast({
                      title: '发布成功',
                    })
                  },
                })
              }
            }
          })
      },100)
        }
      }
    })
  }

})